|
Return value optimization, or simply RVO, is a compiler optimization technique that involves eliminating the temporary object created to hold a function's return value. In C++, it is particularly notable for being allowed to change the observable behaviour of the resulting program. == Summary == In general, the C++ standard allows a compiler to perform any optimization, provided the resulting executable exhibits the same observable behaviour ''as if'' (i.e. pretending) all the requirements of the standard have been fulfilled. This is commonly referred to as the "as-if rule".〔ISO/IEC (2003). ''ISO/IEC 14882:2003(E): Programming Languages - C++ §1.9 Program execution ()'' para. 1〕 The term ''return value optimization'' refers to a special clause in the C++ standard that goes even further than the "as-if" rule: an implementation may omit a copy operation resulting from a return statement, even if the copy constructor has side effects.〔ISO/IEC (2003). ''ISO/IEC 14882:2003(E): Programming Languages - C++ §12.8 Copying class objects ()'' para. 15〕 The following example demonstrates a scenario where the implementation may eliminate one or both of the copies being made, even if the copy constructor has a visible side effect (printing text).〔 The first copy that may be eliminated is the one where C() is copied into the function f 's return value. The second copy that may be eliminated is the copy of the temporary object returned by f to obj .#include struct C }; C f() int main() Depending upon the compiler, and that compiler's settings, the resulting program may display any of the following outputs: Hello World! Hello World! Hello World! 抄文引用元・出典: フリー百科事典『 ウィキペディア(Wikipedia)』 ■ウィキペディアで「return value optimization」の詳細全文を読む スポンサード リンク
|